<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/avatar.png" alt="" />
      </div>
      <el-card style="width: 400px">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          @keyup.enter.native="submitForm('ruleForm')"
          class="login_form"
        >
          <!-- 账户 -->
          <el-form-item label="账户:" prop="userName">
            <el-input
              style="width: 240px"
              placeholder="请输入用户名"
              v-model="ruleForm.userName"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item label="密码:" prop="password">
            <el-input
              style="width: 240px"
              placeholder="请输入密码"
              v-model="ruleForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <!-- 按钮组 -->
          <el-form-item class="btnss">
            <el-button
              type="primary"
              @click="submitForm('ruleForm')"
              :disabled="!canSubmit"
            >
              登录
            </el-button>
            <el-button type="info" @click="resetForm('ruleForm')"
              >取消</el-button
            >
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      ruleForm: {
        userName: '',
        password: '',
      },

      rules: {
        userName: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
        ],
      },
    }
  },
  //计算是否输入值
  computed: {
    canSubmit() {
      const { userName, password } = this.ruleForm
      return Boolean(userName && password)
    },
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (
            this.ruleForm.userName === 'admin' &&
            this.ruleForm.password === '123456'
          ) {
            localStorage.setItem('token', 'xiaoyuan')
            this.$router.push('/home')
          } else {
            alert('用户名或密码错误')
          }
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // login() {
    //   if (this.userName === 'admin' && this.password === '123456') {
    //     this.$router.push('/home')
    //   } else {
    //     alert('用户名或密码错误')
    //   }
    //   // axios({
    //   //   type: 'get',
    //   //   url: '请求的接口路径',
    //   //   params: { username: this.phone, password: this.password },
    //   // }).then((res) => {
    //   //   console.log(res)
    //   //   localStorage.setItem('token', this.phone)
    //   //   this.$router.push({
    //   //     name: 'Home',
    //   //   })
    //   // })
    // },
  },
}
</script>
<style>
.login_container {
  height: 100vh;
  background-image: url(../assets/login_backgroud.jpg);

  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login_box {
  width: 400px;
  height: 317px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
}
.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
}
.avatar_box img {
  height: 100%;
  width: 100%;
  border-radius: 50%;
  background-color: #eee;
}

.el-form-item:last-child {
  margin-bottom: 0;
  background-color: #fff;
}

.btnss {
  display: flex;
  justify-content: center;
  align-items: center;
}
.login_form {
  position: center;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;

  /* margin-left: 25px; */
}
</style>
